package ru.tatintec.gwttest.client.presenter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.moxieapps.gwt.highcharts.client.Chart;
import org.moxieapps.gwt.highcharts.client.ToolTip;
import org.moxieapps.gwt.highcharts.client.ToolTipData;
import org.moxieapps.gwt.highcharts.client.ToolTipFormatter;
import ru.tatintec.gwttest.client.service.ServiceHelper;
import ru.tatintec.gwttest.client.view.EmployeeReportComposite;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;

public class EmployeeReportPresenter {

	private EmployeeReportComposite ui; 

	public EmployeeReportPresenter() {
		ui = new EmployeeReportComposite();
		
		ui.getListBox().addChangeHandler(new ChangeHandler() {
			@Override
			public void onChange(ChangeEvent event) {
				ListBox source = (ListBox) event.getSource(); 
				if ("0".equals(source.getValue(source.getSelectedIndex()))) {
					showGroupingByBirthYear();
				}
				if ("1".equals(source.getValue(source.getSelectedIndex()))) {
					showGroupingByEmploymentDate();
				} 
			}
		});
		
		showGroupingByBirthYear();
	}

	
	
	public void showGroupingByBirthYear() {
		final Chart chart = ui.getChart();
		chart.removeAllSeries();
				
		chart.setToolTip(new ToolTip()  
	        .setFormatter(new ToolTipFormatter() {  
	            public String format(ToolTipData toolTipData) {  
	                return toolTipData.getYAsLong() + " employees was born in " + toolTipData.getXAsString() + " year";  
	            }  
	        })
		);

		ServiceHelper.getEmployeeReportService().getNumberOfEmployeeByBirthYear(new AsyncCallback<Map<Integer, Integer>>() {
			@Override
			public void onSuccess(Map<Integer, Integer> result) {
			    
				List<String> x = new ArrayList<String>(); 
				List<Integer> y = new ArrayList<Integer>(); 
				
				for (Integer key : result.keySet()) {
					x.add(""+key);
					y.add(result.get(key));
				}
				
			    chart.getXAxis()  
	        		.setCategories(false, x.toArray((new String[result.size()])));
			    
			    chart.addSeries(chart.createSeries()  
			    		.setName("Grouped by birth year")  
			        .setPoints(y.toArray(new Integer[result.size()]), false)
			    );
			    
			    chart.redraw();
			    
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				Window.alert(caught.getMessage());
			}
		});
	}
	
	
	public void showGroupingByEmploymentDate() {
		
		final Chart chart = ui.getChart();
		chart.removeAllSeries();
		chart.setToolTip(new ToolTip()  
	        .setFormatter(new ToolTipFormatter() {  
	            public String format(ToolTipData toolTipData) {  
	                return toolTipData.getYAsLong() + " employees hired at " + toolTipData.getXAsString();  
	            }  
	        })
		);

		ServiceHelper.getEmployeeReportService().getNumberOfEmployeeByEmploymentYear(new AsyncCallback<Map<String,Integer>>() {
			
			@Override
			public void onSuccess(Map<String, Integer> result) {
				List<String> x = new ArrayList<String>(); 
				List<Integer> y = new ArrayList<Integer>(); 
				
				for (String key : result.keySet()) {
					x.add(key);
					y.add(result.get(key));
				}
				
			    chart.getXAxis()  
	        		.setCategories(false, x.toArray((new String[result.size()])));
			    
			    chart.addSeries(chart.createSeries()  
			        .setName("Grouped by employment date")  
			        .setPoints(y.toArray(new Integer[result.size()]), false)
			    );
			    chart.redraw();
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				Window.alert(caught.getMessage());
			}
		});
	}
	
	
	public Widget getUi() {
		return ui;
	}

}
